using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using SypEngine;
using SypConst.Garbage;
using SypConst.Path;
using SypEngine.SiteConfig;

namespace SypEngine
{
    public class SypCategoryVisitor : SypVisitor
    {
        public SypCategoryVisitor(SetText WriteMethod, VisitorCallBack[] Callbacks)
            : base (WriteMethod, Callbacks)
        {
        }

      

        public void VisitShowbrowserMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 1)
            {
                int SiteId = -1;
                if (int.TryParse(parameters[1], out SiteId))
                {
                    if (Callbacks.Length > 0)
                        Callbacks[0](SiteId);
                }
                else
                    OutputMethod("Showbrowser-category : cannot convert " + SiteId + "in int type.", Output.ERROR);
            }
            else
                OutputMethod("Showbrowser-category : missing parameters", Output.ERROR);
        }

        public void VisitAddfromfileMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 3)
            {
                int SiteId = 0;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                }
                catch (Exception)
                {
                    OutputMethod("[" + SiteId + "]Addfromfile-category : first parameter is not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                string FolderToImport = cleanArg(parameters[2]);
                
                string FolderName = cleanArg(parameters[3]);

                if (mySite != null)
                {
                    if (Directory.Exists(FolderToImport))
                    {
                        if (mySite.CategorysList == null)
                            mySite.CategorysList = new List<Category>();
                        else
                        {
                            foreach (Category cat in mySite.CategorysList)
                            {
                                if (cat.AlbumList != null)
                                {
                                    foreach (Album alb in cat.AlbumList)
                                    {
                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                        
                                        //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, alb.id), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    }
                                }
                            }
                        }

                        // Add category folder
                        DirectoryInfo FolderInfo = new DirectoryInfo(FolderToImport);
                        
                        Category myCategory = null;
                        if (FolderName != "")
                            myCategory = new Category(FolderName);
                        else
                            myCategory = new Category(FolderInfo.Name);

                        mySite.CategorysList.Add(myCategory);
                        //string categoryLocal = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.CATEGORY_FOLDER, null, mySite.SiteId, myCategory.Id);
                        //string categoryOnline = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.CATEGORY_FOLDER, null, mySite.SiteId, myCategory.Id);
                        //SiteObj.AddAction(mySite.SiteId, categoryLocal, SyncAction.ADD_FOLDER, SyncMode.LOCAL);
                        //SiteObj.AddAction(mySite.SiteId, categoryOnline, SyncAction.ADD_FOLDER, SyncMode.ONLINE);
                        SypGarbage.free(FolderInfo);

                        //add Album folder and page
                        string[] DirectoryAlbums = Directory.GetDirectories(FolderToImport);
                        foreach (string directory in DirectoryAlbums)
                        {
                            FolderInfo = new DirectoryInfo(directory);
                            //Add album folder
                            Album newAlbum = new Album(FolderInfo.Name, directory);
                            //string albumLocalDir = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_FOLDER, null, mySite.SiteId, myCategory.Id, newAlbum.id);
                            //string albumOnlineDir = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, mySite.SiteId, myCategory.Id, newAlbum.id);
                            //SiteObj.AddAction(mySite.SiteId, albumLocalDir, SyncAction.ADD_FOLDER, SyncMode.LOCAL);
                            //SiteObj.AddAction(mySite.SiteId, albumOnlineDir, SyncAction.ADD_FOLDER, SyncMode.ONLINE);


                            //Add album page
                            SypPage myPage = new SypPage(newAlbum.id, SiteId, OutputMethod, mySite.Language);
                            SiteObj.AddAction(mySite.SiteId, myPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                            //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + myCategory.Id + "/" + SypGarbage.ALBUM_PREF_DIR + newAlbum.id + "/" + SypGarbage.ALBUM_PAGE_PREF + newAlbum.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                            SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, myCategory.Id, newAlbum.id), SyncAction.ADD_FILE, SyncMode.ONLINE);
                            if (myCategory.AlbumList == null)
                                myCategory.AlbumList = new List<Album>();
                             myCategory.AlbumList.Add(newAlbum);
                            SypGarbage.free(FolderInfo);

                            //Add picture page
                            string[] files = SypGarbage.GetFiles(directory, SypGarbage.PICTURE_FILTER);
                            foreach (string file in files)
                            {
                                //if (SypGarbage.CheckPictureFile(file))
                                //{
                                    FileInfo PictureInfo = new FileInfo(file);
                                    Picture newPicture = new Picture(PictureInfo.Name, file);
                                    SypGarbage.free(PictureInfo);
                                    //Copy picture
                                    SiteObj.AddAction(mySite.SiteId, new string[] { file, newPicture.Id.ToString() } , SyncAction.COPY_PICTURE, SyncMode.LOCAL);
                                    
                                    //SiteObj.AddAction(mySite.SiteId, SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + newPicture.Id + SypGarbage.PICTURE_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                    SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                    //SiteObj.AddAction(mySite.SiteId, SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + newPicture.Id + SypGarbage.THUMBNAIL_EXTENSION, SyncAction.COPY_PICTURE, SyncMode.ONLINE);
                                    SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, newPicture.Id), SyncAction.COPY_PICTURE, SyncMode.ONLINE);

                                    if (newAlbum.PicturesList == null)
                                        newAlbum.PicturesList = new List<Picture>();
                                    newAlbum.PicturesList.Add(newPicture);
                                //}
                            }
                        }
                        if (myCategory.AlbumList != null && myCategory.AlbumList.Count > 0) 
                            mySite.AlbumIdToReload = myCategory.AlbumList[0].id;

                    }
                    else
                        OutputMethod("[" + SiteId + "]Addfromfile-category : directory not found, "+FolderToImport, Output.ERROR);
                }
                else
                    OutputMethod("[" + SiteId + "]Addfromfile-category : unknow SiteId", Output.ERROR);
            }
            else
                OutputMethod("Addfromfile-category : missing parameters", Output.ERROR);
        }

        public override void VisitAddMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 2)
            {
                int SiteId = 0;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                }
                catch (Exception)
                {
                    OutputMethod("[" + SiteId + "]Add-category : first parameter is not a number", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                if (mySite != null)
                {
                    if (mySite.CategorysList == null)
                        mySite.CategorysList = new List<Category>();
                    else
                    {
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                    SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                    //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + alb.Id + "/" + SypGarbage.ALBUM_PREF_DIR + sect.id + "/" + SypGarbage.ALBUM_PAGE_PREF + sect.id + SypGarbage.PAGE_EXTENSION, SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                }
                            }
                        }
                    }

                    
                    Category myCategory = new Category(cleanArg(parameters[2]));
                    mySite.CategorysList.Add(myCategory);

                    //string categoryLocal = mySite.LocalDirectory + SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + myCategory.Id;
                    //string categoryLocal = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.CATEGORY_FOLDER, null, mySite.SiteId, myCategory.Id);
                    //string categoryOnline = SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + myCategory.Id;
                    //string categoryOnline = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.CATEGORY_FOLDER, null, -1, myCategory.Id);

                    //SiteObj.AddAction(mySite.SiteId, categoryLocal, SyncAction.ADD_FOLDER, SyncMode.LOCAL);
                    //SiteObj.AddAction(mySite.SiteId, categoryOnline, SyncAction.ADD_FOLDER, SyncMode.ONLINE);
                }
                else
                    OutputMethod("[" + SiteId + "]Add-category : unknow SiteId", Output.ERROR);
            }
            else
                OutputMethod("Add-category : missing parameters", Output.ERROR);
        }

        public override void VisitEditMethod(SypSiteHandler SiteObj,  string[] parameters)
        {
            OutputMethod("Visit EditMethod category", Output.OUT);
        }

        public override void VisitSupprMethod(SypSiteHandler SiteObj, string[] parameters)
        {
            if (parameters.Length > 2)
            {
                int SiteId = -1;
                int catId = -1;
                int AlbumVisitedId = -1;
                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                    catId = int.Parse(cleanArg(parameters[2]));

                    if (parameters.Length > 3)
                        AlbumVisitedId = int.Parse(cleanArg(parameters[3]));
                }
                catch (Exception)
                {
                    OutputMethod("[" + SiteId + "]Suppr-category : one or more parameters are not numbers", Output.ERROR);
                    return;
                }
                SypSite mySite = SiteObj.GetSiteConfig(SiteId);
                OutputMethod(SiteId.ToString() + SypGarbage.COMMAND_SEPARATOR + "0" + SypGarbage.COMMAND_SEPARATOR + "100", Output.LOCAL_STEP);
                if (mySite != null)
                {
                  
                    if (mySite.CategorysList != null)
                    {
                        bool ReloadVisitedAlbum = true;
                        Category catToRemove = null;
                        //string categoryOnline = SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + AlbId + "/";
                        //string categoryOnline = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.CATEGORY_FOLDER, null, -1, AlbId);
                        foreach (Category cat in mySite.CategorysList)
                        {
                            if (cat.Id.Equals(catId))
                                catToRemove = cat;
                            if (cat.AlbumList != null)
                            {
                                foreach (Album alb in cat.AlbumList)
                                {
                                    if (!cat.Id.Equals(catId))
                                    {
                                        SypPage EditAlbumPage = new SypPage(alb.id, SiteId, OutputMethod, mySite.Language);
                                        SiteObj.AddAction(mySite.SiteId, EditAlbumPage, SyncAction.ADD_FILE, SyncMode.LOCAL);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id), SyncAction.ADD_FILE, SyncMode.ONLINE);
                                    }
                                    else
                                    {
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ALBUM_PAGE, null, mySite.SiteId, cat.Id, alb.id), SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                        SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_PAGE, null, -1, cat.Id, alb.id), SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                        if (alb.id == AlbumVisitedId)
                                            ReloadVisitedAlbum = false;
                                        
                                    }
                                    if (alb.PicturesList != null)
                                    {
                                        foreach (Picture pict in alb.PicturesList)
                                        {
                                            if (cat.Id.Equals(catId))
                                            {
                                                //Suppr thumbnail and image
                                                //string thumbnailfile = mySite.LocalDirectory + SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + pict.Id + SypGarbage.THUMBNAIL_EXTENSION;
                                                string thumbnailfile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.THUMBNAIL_FILE, null, mySite.SiteId, -1, -1, pict.Id);
                                                //string picturefile = mySite.LocalDirectory + SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + pict.Id + SypGarbage.PICTURE_EXTENSION;
                                                string picturefile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PICTURE_FILE, null, mySite.SiteId, -1, -1, pict.Id);
                                                SiteObj.AddAction(mySite.SiteId, thumbnailfile, SyncAction.SUPPR_FILE, SyncMode.LOCAL);
                                                SiteObj.AddAction(mySite.SiteId, picturefile, SyncAction.SUPPR_FILE, SyncMode.LOCAL);

                                                //thumbnailfile = SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + pict.Id + SypGarbage.THUMBNAIL_EXTENSION;
                                                thumbnailfile = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.THUMBNAIL_FILE, null, -1, -1, -1, pict.Id);
                                                //picturefile = SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + pict.Id + SypGarbage.PICTURE_EXTENSION;
                                                picturefile = SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FILE, null, -1, -1, -1, pict.Id);
                                                SiteObj.AddAction(mySite.SiteId, thumbnailfile, SyncAction.SUPPR_FILE, SyncMode.ONLINE);
                                                SiteObj.AddAction(mySite.SiteId, picturefile, SyncAction.SUPPR_FILE, SyncMode.ONLINE);

                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (catToRemove != null)
                        {
                            mySite.CategorysList.Remove(catToRemove);
                            //SiteObj.AddAction(mySite.SiteId, SypGarbage.PAGE_FOLDER + SypGarbage.CATEGORY_PREF_DIR + AlbId, SyncAction.SUPPR_FOLDER, SyncMode.ONLINE);
                            //SiteObj.AddAction(mySite.SiteId, SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.ALBUM_FOLDER, null, -1, AlbId), SyncAction.SUPPR_FOLDER, SyncMode.ONLINE);
                        }
                        if (ReloadVisitedAlbum)
                            mySite.AlbumIdToReload = AlbumVisitedId;
                    }
                    else
                        OutputMethod("[" + SiteId + "]Suppr-category : unknown category", Output.ERROR);
                }
                else
                    OutputMethod("[" + SiteId + "]Suppr-category : unknow SiteId", Output.ERROR);
            }
            else
                OutputMethod("Suppr-category : missing arguments", Output.OUT);
        }
    }
}
